#include "table.h"
#include <iostream>
#include <algorithm>
#include <boost/foreach.hpp>

table::table(int n){

	players.resize(n);
}

deck table::getDeck(){

	return deckSet;
}

std::vector<card> table::getGroundCards(){

	return groundCards;
}

std::vector<player> table::getPlayers(){

	return players;

}

void table::preflop(){

	using std::vector;
	card playerCard;
	vector<player>::iterator it;

	for (int i=1;i<=2;++i){
		for (it=players.begin();it!=players.end();++it){
			playerCard=deckSet.getRandomCard();
			it->setCard(playerCard);
		}
	}

//	this->show("preflop");

}

void table::flop(){

	using std::vector;

	deckSet.getRandomCard();
	card groundCard;
	for (int i=1;i<=3;++i){
		groundCard=deckSet.getRandomCard();
		groundCards.push_back(groundCard);
	}

	vector<player>::iterator itp;
/*	for (itp=players.begin();itp!=players.end();++itp){

		itp->flopHand(groundCards);
	}
*/

	BOOST_FOREACH( player& p, players )
	{
		p.flopHand(groundCards);
	}
//	this->show("flop");

}

void table::turn(){

	deckSet.getRandomCard();	

	card groundCard;
	groundCard=deckSet.getRandomCard();
	groundCards.push_back(groundCard);

	std::vector<player>::iterator itp=players.begin();
	for(;itp!=players.end();++itp){
		itp->turnHand(groundCard);
	}

//	this->show("turn");

}

void table::river(){

	deckSet.getRandomCard();	

	card groundCard;
	groundCard=deckSet.getRandomCard();
	groundCards.push_back(groundCard);

	std::vector<player>::iterator itp=players.begin();
	for(;itp!=players.end();++itp){
		itp->riverHand(groundCard);
	}
//	this->show("river");

}


int table::winner(){

	sort(players.begin(),players.end(),playerSort);
	return players[0].getPlayerNumber();

}


void table::show(std::string stepFlag){

	vector<player>::iterator itp;
	vector<card>::iterator itc;

	if (stepFlag=="preflop"){

		std::cout<<std::endl<<std::endl;
		std::cout<<"***********************************************"<<std::endl;
		std::cout<<"====================PreFLOP===================="<<std::endl;
		std::cout<<"***********************************************"<<std::endl;
		std::cout<<std::endl<<std::endl;
	
		vector<card> playerCards;
	
		for (itp=players.begin();itp!=players.end();++itp){
			playerCards=itp->getCards();
			std::cout<<"Player "<<itp-players.begin()+1<<" :";
			for (itc=playerCards.begin();itc!=playerCards.end();++itc){
				std::cout<<(*itc)<<"          ";
			}
			std::cout<<std::endl;
		}
	}

	else{
		if (stepFlag=="flop"){
			std::cout<<std::endl<<std::endl;
			std::cout<<"***********************************************"<<std::endl;
			std::cout<<"====================FLOP===================="<<std::endl;
			std::cout<<"***********************************************"<<std::endl;
			std::cout<<std::endl<<std::endl;
		}

		if (stepFlag=="turn"){
			std::cout<<std::endl<<std::endl;
			std::cout<<"***********************************************"<<std::endl;
			std::cout<<"======================TURN====================="<<std::endl;
			std::cout<<"***********************************************"<<std::endl;
			std::cout<<std::endl<<std::endl;
		}

		if (stepFlag=="river"){
			std::cout<<std::endl<<std::endl;
			std::cout<<"***********************************************"<<std::endl;
			std::cout<<"=====================RIVER====================="<<std::endl;
			std::cout<<"***********************************************"<<std::endl;
			std::cout<<std::endl<<std::endl;
		}

		std::cout<<std::endl<<"*********Ground Cards*********"<<std::endl;
		vector<card>::iterator itg;
		for (itg=groundCards.begin();itg!=groundCards.end();++itg){
			std::cout<<(*itg)<<std::endl;
		}

		sort(players.begin(),players.end(),playerSort);
		vector<card> fiveCards(5);
		for (itp=players.begin();itp!=players.end();++itp){
			fiveCards=itp->getFiveCards();
			std::cout<<std::endl<<"Player "<<itp-players.begin()+1<<"=============>"<<std::endl;

			for (itc=fiveCards.begin();itc!=fiveCards.end();++itc){
				std::cout<<(*itc)<<std::endl;
			}

			std::cout<<"********  ";
			std::cout<<itp->getPlayerHandName();
			std::cout<<"   ********";
			std::cout<<std::endl;
		

		}

	}

	

}